package com.shike.core.storage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StorageConfig {

    private final String STORAGE_TYPE = "qiniu";

    @Autowired
    private StorageProperties storageProperties;

    /**
     * 配置上传文件服务类
     * @return
     */
    @Bean
    public StorageService storageService() {
        StorageService storageService = new StorageService();
        if (this.STORAGE_TYPE.equals(storageProperties.getActive())){
            storageService.setStorage(qiniuStorage());
        }
        return storageService;
    }

    /**
     * 配置七牛
     * @return
     */
    @Bean
    public QiniuStorage qiniuStorage() {
        QiniuStorage qiniuStorage = new QiniuStorage();
        StorageProperties.Qiniu qiniu = this.storageProperties.getQiniu();
        qiniuStorage.setAccessKey(qiniu.getAccessKey());
        qiniuStorage.setSecretKey(qiniu.getSecretKey());
        qiniuStorage.setBucketName(qiniu.getBucketName());
        qiniuStorage.setEndpoint(qiniu.getEndpoint());
        return qiniuStorage;
    }
}
